﻿Imports CGR.SEGUR.BE
Imports System.Transactions
Imports Owasp.Esapi

Imports CGR.SEGUR.UTIL

Public Class EncriptacionBL

    ''' <summary>
    ''' Generacion de contrasena para los sistemas que consuman los servicios de la CGR.
    ''' </summary>
    ''' <param name="tipo">Sistemas:T, Entidad: C</param>
    ''' <param name="codigoSistema">Codigo Aplicación</param>
    ''' <returns>contrasena</returns>
    ''' <remarks></remarks>
    Public Function GenerarContrasena(ByVal tipo As String, ByVal codigoSistema As String) As String

        Dim contrasena As String = ""
        contrasena = Esapi.Encryptor.Encrypt(tipo + codigoSistema + Esapi.Randomizer.GetRandomString(20, Owasp.Esapi.CharSetValues.Alphanumerics))
        Return contrasena

    End Function

    Public Function GenerarCredenciales(ByVal dato As String) As String

        Dim encrypt As String = ""
        'encrypt = Esapi.Encryptor.Encrypt(dato) + "|" + Esapi.Encryptor.Encrypt(Esapi.Randomizer.GetRandomString(30, Owasp.Esapi.CharSetValues.Alphanumerics).ToString)
        encrypt = Esapi.Encryptor.Encrypt(dato + Esapi.Encryptor.Encrypt(Esapi.Randomizer.GetRandomString(30, Owasp.Esapi.CharSetValues.Alphanumerics).ToString))
        Return encrypt

    End Function

    Public Function ContrasenaEncriptada(ByVal obj As ContrasenaBE, ByVal usuario As String, ByVal token As String) As String

        Dim encrypt As String = ""

        If String.IsNullOrEmpty(obj.Clave) Then
            obj.Clave = New UsuarioBL().TextoAleatorio(6)
        End If

        If String.IsNullOrEmpty(obj.UsuarioCodigo) Then
            Return encrypt
        End If

        Using srv As New srvUsuarioENT.UsuarioENTClient
            encrypt = srv.ContrasenaEncriptar(obj, usuario, token)
        End Using

        Return encrypt

    End Function

End Class
